package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"jd/config"
	"mime/multipart"
)

func QiNiuYun(file multipart.File, m *multipart.FileHeader, filename string) (string, error) {

	appConfig := config.AppConfig.QiNiuYun

	//accessKey := "hP4x8xeC2oxD9Rs5PG-oC9-xe7tZxaqxxdVdYXx_"
	//secretKey := "pdjokCLfYFVqUraxmY58JSZ4zQWzoovp-4wPcPBM"
	accessKey := appConfig.AccessKey
	secretKey := appConfig.SecretKey
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := "dongbo1"
	key := filename
	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadReader(context.Background(), file, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		FileName:   key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	return "http://sm3mebmf1.hn-bkt.clouddn.com" + filename, err
}
